package com.none.staff.view.activitys

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.view.ViewPager
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.none.staff.R
import com.none.staff.view.adapter.HomeFragmentPagerAdapter
import com.none.staff.view.fragments.AssistantFragment
import com.none.staff.view.fragments.MeFragment
import com.none.staff.view.fragments.NewsRoomFragment
import com.peter.electronicmall.base.BaseActivity
import com.tencent.bugly.proguard.i


class HomeActivity : BaseActivity(), NewsRoomFragment.OnFragmentInteractionListener, AssistantFragment.OnFragmentInteractionListener
        , MeFragment.OnFragmentInteractionListener {


    lateinit var tab: TabLayout
    lateinit var viewPager: ViewPager
    lateinit var fragmentManager: FragmentManager
    lateinit var fragmentList: ArrayList<Fragment>
    lateinit var pageAdapter: HomeFragmentPagerAdapter
    private lateinit var titles: ArrayList<String>

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, HomeActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun getLayoutId(): Int {
        return R.layout.activity_home
    }

    override fun initView() {

        tab = findViewById(R.id.tab_home)
        //1.MODE_SCROLLABLE模式
//        tab.tabMode = TabLayout.MODE_SCROLLABLE
        viewPager = findViewById(R.id.vp_home)
        viewPager.adapter = pageAdapter
        tab.setupWithViewPager(viewPager)
//        setTabView()
//        addFragmentListData()
    }

    override fun initData() {
        fragmentManager = supportFragmentManager
        fragmentList = ArrayList()
        fragmentList.add(NewsRoomFragment.newInstance("NewsRoomFragment", "NewsRoomFragment"))
        fragmentList.add(AssistantFragment.newInstance("AssistantFragment", "AssistantFragment"))
        fragmentList.add(MeFragment.newInstance("MeFragment", "MeFragment"))
        titles = ArrayList()
        titles.add("PageOne")
        titles.add("PageTwo")
        titles.add("PageThree")
        pageAdapter = HomeFragmentPagerAdapter(fragmentManager, titles, fragmentList)

    }

    @SuppressLint("SetTextI18n")
    private fun setTabView() {
        val tabCount: Int = tab.tabCount
        for (i in 0 until tabCount) {
            val tabL: TabLayout.Tab = tab.getTabAt(i)!!
            //通过相应的布局文件获取view
            val view = View.inflate(this, R.layout.tab_view, null)
            val iv = view.findViewById(R.id.iv) as ImageView
            val tv = view.findViewById(R.id.tv) as TextView
            //设置tab栏文字
            tv.text = "T$i"
            //设置tab图片
            iv.setImageResource(R.mipmap.ic_launcher)
            //给tab设置view
            tabL.customView = view
        }
    }

    override fun onReConnect() {

    }

    override fun onFragmentInteractionN(uri: Uri) {

    }

    override fun onFragmentInteractionA(uri: Uri) {

    }

    override fun onFragmentInteractionM(uri: Uri) {

    }

}
